package com.example.androidkt.sixth

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.androidkt.databinding.ActivityRoomBinding

class RoomActivity : AppCompatActivity() {

    val TAG = this.javaClass.simpleName

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val binding = ActivityRoomBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val user1 = User("X1","Y1",12)
        val user2 = User("X2","Y2",12)

        val userDao = AppDataBase.getDatabase(context = this).userDao()


        binding.insert.setOnClickListener {
            user1.id = userDao.insertUser(user1)
            user2.id = userDao.insertUser(user2)
        }

        binding.delete.setOnClickListener {
            userDao.deleteUserByName("X2")
        }

        binding.update.setOnClickListener {
            user1.age = 50
            userDao.updateUser(user1)
        }

        binding.query.setOnClickListener {
            for (user in userDao.loadAllUsers()){
                Log.i(TAG,user.toString())
            }
        }


        //须在子线程操作
        /*thread {

        }*/

    }


}